! Roel Matthysen
! s0202264
! Finale versie Project Deel 1

module IO

	implicit none
	private

	integer, parameter :: doubleprecision = kind(0.d0)	
	public :: printmatrix, readmatrix, readsize

	
	contains
	! Duale routine van readmatrix
	subroutine printmatrix(A)
		real(doubleprecision), dimension(:,:) :: A
		integer :: i
		write(6,*) size(A,1)
		do i=1,size(A,1)
			write(6,*) A(i,:)
		end do
	end subroutine
	
	! Met optioneel optunit argument dat van een andere stream inleest
	function readmatrix(optunit) result(A)
		real(doubleprecision),allocatable :: A(:,:)
		integer :: i,n,inputunit
		integer, optional :: optunit
		if(present(optunit)) then
			inputunit = optunit
		else
			inputunit = 5
		endif
		read(inputunit,*) n
		allocate(A(n,n))
		do i=1,n
			read(inputunit,*) A(i,:)
		enddo
	end function

	! Leest enkel grootte van de matrix, zonder de cursor in de file te verplaatsen
	function readsize(inputunit) result(n)
		integer :: n,inputunit
		read(inputunit,*) n
		rewind(inputunit)
	end function

end module
